﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EpiNEXT.Core;
using EpiNEXT.Core.Fields;

namespace EpiNEXT.WPF.View.Pages
{
    /// <summary>
    /// Interaction logic for PrintViewer.xaml
    /// </summary>
    public partial class PrintViewer : UserControl
    {
        public PrintViewer()
        {
            InitializeComponent();
        }

        private void LoadForm() 
        {
            document = new FixedDocument();
            documentViewer.Document = document;

            ProjectViewModel vm = this.DataContext as ProjectViewModel;

            foreach (PageViewModel pageVM in vm.SelectedForm.PagesView)
            {
                PageContent pageContent = new PageContent();
                FixedPage fixedPage = new FixedPage();

                Canvas canvas = new Canvas();
                canvas.IsHitTestVisible = false;
                canvas.Margin = new Thickness(35, 35, 35, 35);
                canvas.Width = 738;
                canvas.Height = 990;
                canvas.DataContext = vm;

                ControlFactory controlFactory = new ControlFactory(BindingMode.OneWay, ControlCreationMode.Printing);
                Dictionary<UIElement, IField> controlsForPage = controlFactory.CreateControlsForPage(pageVM);

                foreach (KeyValuePair<UIElement, IField> controlFieldPair in controlsForPage)
                {
                    canvas.Children.Add(controlFieldPair.Key);
                }

                fixedPage.Children.Add(canvas);

                // to fix an odd crashes for image fields, don't allow mouse over on images... odd
                Canvas canvas2 = new Canvas();
                canvas2.IsHitTestVisible = true;
                canvas2.Margin = new Thickness(35, 35, 35, 35);
                canvas2.Width = 738;
                canvas2.Height = 990;
                canvas2.Background = Brushes.Transparent;

                fixedPage.Children.Add(canvas2);

                pageContent.Child = fixedPage;
                document.Pages.Add(pageContent);
            }
        }

        private void CloseForm()
        {
            if (document != null)
            {
                foreach (PageContent pageContent in document.Pages) 
                {
                    if (pageContent.Child != null)
                    {
                        FixedPage fixedPage = pageContent.Child as FixedPage;
                        Canvas canvas1 = fixedPage.Children[0] as Canvas;
                        Canvas canvas2 = fixedPage.Children[1] as Canvas;

                        canvas1.Children.Clear();
                        canvas2.Children.Clear();

                        canvas1 = null;
                        canvas2 = null;

                        fixedPage.Children.Clear();
                    }
                }

                document = null;
            }
        }

        private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue == false)
            {
                if (this.Visibility == System.Windows.Visibility.Collapsed || this.Visibility == System.Windows.Visibility.Hidden)
                {
                    CloseForm();
                }
            }
            else
            {
                LoadForm();
            }
        }
    }
}
